package com.ym.medical.service.impl;

import com.ym.core.utils.tkmybatis.BaseSVImpl;

import com.ym.medical.dao.DoctorNavInforDao;
import com.ym.medical.domain.entity.DoctorNavInforEntity;
import com.ym.medical.domain.param.DoctorNavInforParam;
import com.ym.medical.service.IDoctorNavInforSV;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ComputerName:LAPTOP-9FVRPDCS
 * Author:lenovo
 * Date: 2019-07-16
 * Time: 10:47:42
 * 医生信息表服务实现类
 */
@Service
@Transactional( rollbackFor = Exception.class)
public class DoctorNavInforSVImpl extends BaseSVImpl<DoctorNavInforEntity, DoctorNavInforDao> implements IDoctorNavInforSV {

    @Override
    public Map<String, Object> uploadHisDeptInfo(List<DoctorNavInforEntity> doctorNavInfors,Long userId) {
        HashMap<String, Object> result = new HashMap<>();
        ArrayList<Object> failList = new ArrayList<>();
        Integer rows = 0;
        DoctorNavInforEntity doctorNavInforModel = new DoctorNavInforEntity();
        List<DoctorNavInforEntity> list;
        for (DoctorNavInforEntity doctorNavInfor : doctorNavInfors) {
            try {
                doctorNavInforModel.setHosCode(doctorNavInfor.getHosCode());
                doctorNavInforModel.setDoctorStaffNo(doctorNavInfor.getDoctorStaffNo());
                list = mapper.select(doctorNavInforModel);
                //修改
                if (!list.isEmpty()) {
                    doctorNavInfor.setModifier(userId);
                    mapper.updateByExampleSelective(doctorNavInfor, doctorNavInforModel);
                } else {//新增
                    doctorNavInfor.setCreator(userId);
                    mapper.insertSelective(doctorNavInfor);
                }
                rows+=1;
            }catch (Exception e){
                failList.add(doctorNavInfor);
            }
        }
        result.put("success",rows);
        result.put("fail",failList);
        return result;
    }

    @Override
    public DoctorNavInforEntity selectSingleByModelWithBlobs(DoctorNavInforParam model) {
        return mapper.selectSingleByModelWithBlobs(model);
    }

    @Override
    public int batchUpdatePhoto(List<DoctorNavInforEntity> doctorNavInfors) {
        for(DoctorNavInforEntity doctorNavInfor: doctorNavInfors) {
              mapper.updateByPrimaryKeySelective(doctorNavInfor);
        }
        return 0;
    }
}
